package com.example.springsecuritycustomauthenticationfailurehandler.controller;

import com.example.springsecuritycustomauthenticationfailurehandler.service.IAuthenticationFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
public class WebController {

    @Autowired
    private IAuthenticationFacade authenticationFacade;

    @GetMapping(value = "/index")
    @ResponseStatus(HttpStatus.OK)
    public String index(){
        return "index";
    }

    @GetMapping(value = "/getUserBySecurityContextHolder",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getUserBySecurityContextHolder(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            String username = authentication.getName();
            return new ResponseEntity<>(username,HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    @GetMapping(value = "/getUserByPrincipal",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getUserByPrincipal(Principal principal){
        if (principal == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
        return new ResponseEntity<>(principal.getName(),HttpStatus.OK);
    }

    @GetMapping(value = "/getUserByAuthentication",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getUserByAuthentication(Authentication authentication){
        if (authentication == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
        return new ResponseEntity<>(authentication.getName(),HttpStatus.OK);
    }

    @GetMapping(value = "/getUserByHttpServletRequest",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getUserByHttpServletRequest(HttpServletRequest request){
        String name = request.getUserPrincipal().getName();
        return new ResponseEntity<>(name,HttpStatus.OK);
    }

    @GetMapping(value = "/getUserByAuthenticationFacade",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getUserByAuthenticationFacade(){
        Authentication authentication = authenticationFacade.getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            return new ResponseEntity<>(authentication.getName(),HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    }
}
